$(function () {
    $("#successDiv").hide();
    $.ajax({
        type: 'POST',
        url: '/userShow.action',
        contentType: 'application/json;charset=utf-8',
        success: function (result) {
            console.log(result.loginId)
            $("#reviewer").val(result.loginId)
        }
    })
    $.ajax({
        type: 'POST',
        async: false,
        url: '/zjj/queryTheProductionPlanToBeReviewed.action',
        contentType: 'application/json;charset=utf-8',
        success: function (result) {
            $.each(result.data, function (i, v) {
                let tr = $("<tr></tr>")
                    .append("<td>" + v.applyId + "</td>")
                    .append("<td>" + v.register + "</td>")
                    .append("<td>" + v.designer + "</td>")
                    .append("<td>" + v.registerTime + "</td>")
                    .append("<td>" + v.remark + "</td>")
                    .append("<td><button value='" + v.applyId + "' data-toggle=\"modal\" data-target=\"#myModal\" class='btn btn-info' onclick='reviewProductionPlan(this)'>审核</button></td>");
                $("#tbodyCommodity").append(tr);
            })
            jQuery.getScript("../js/datatables.init.js")
        }
    })
})

function reviewProductionPlan(e) {
    $.ajax({
        type: 'POST',
        async: false,
        url: '/zjj/queryPendingProductionPlan.action',
        contentType: 'application/json;charset=utf-8',
        data: $(e).val(),
        success: function (result) {
            $("#tbody").html('')
            let curren = result.data[0];
            $("#planOrderNumber").val(curren.applyId)
            $("#registerPerson").val(curren.register)
            $("#checkInTime").val(curren.registerTime)
            $("#reviewTime").val(CurentTime)
            $.each(result.data, function (i, v) {
                let tr = $("<tr></tr>")
                    .append("<td>" + v.productId + "</td>")
                    .append("<td>" + v.productName + "</td>")
                    .append("<td>" + v.productDescribe + "</td>")
                    .append("<td>" + v.amount + "</td>")
                    .append("<td>" + v.constPrice + "</td>");
                $("#tbody").append(tr)
            })
        }
    })
}

$("#through").click(function () {
    let dataJson = {
        'applyId': $("#planOrderNumber").val(),
        'checker': $("#reviewer").val(),
        'checkSuggestion': $("#auditOpinion").val(),
        'checkTime': $("#reviewTime").val()
    }
    $.ajax({
        type: 'POST',
        async: false,
        url: '/zjj/productionPlanApproved.action',
        contentType: 'application/json;charset=utf-8',
        dataType: 'json',
        data:JSON.stringify(dataJson),
        success: function () {
            commonUtil.message(3);
            setTimeout(function () {
                window.location.reload();
            }, 2000)
        }
    })
})


$("#refuse").click(function () {
    let dataJson = {
        'applyId': $("#planOrderNumber").val(),
        'checker': $("#reviewer").val(),
        'checkSuggestion': $("#auditOpinion").val(),
        'checkTime': $("#reviewTime").val()
    }
    $.ajax({
        type: 'POST',
        async: false,
        url: '/zjj/productionPlanRejection.action',
        contentType: 'application/json;charset=utf-8',
        dataType: 'json',
        data:JSON.stringify(dataJson),
        success: function () {
            commonUtil.message(2);
            setTimeout(function () {
                window.location.reload();
            }, 2000)
        }
    })
})

function CurentTime() {
    var now = new Date();
    var year = now.getFullYear();       //年
    var month = now.getMonth() + 1;     //月
    var day = now.getDate();            //日

    var hh = now.getHours();            //时
    var mm = now.getMinutes();          //分

    var clock = year + "-";

    if (month < 10)
        clock += "0";

    clock += month + "-";

    if (day < 10)
        clock += "0";

    clock += day + "T";

    if (hh < 10)
        clock += "0";

    clock += hh + ":";
    if (mm < 10) clock += '0';
    clock += mm;
    return (clock);
}

var commonUtil = {
    /**
     * 弹出消息框
     * @param msg 消息内容
     * @param type 消息框类型（参考bootstrap的alert）
     */
    alert: function (msg, type) {
        if (typeof (type) == "undefined") { // 未传入type则默认为success类型的消息框
            type = "success";
        }
        // 创建bootstrap的alert元素
        if (msg == 1) {
            var divElement = $("<div style='width: 220px' class=\"alert alert-primary \"><svg viewBox=\"0 0 24 24\" width=\"24\" height=\"24\" stroke=\"currentColor\" stroke-width=\"2\" fill=\"none\" stroke-linecap=\"round\" stroke-linejoin=\"round\" class=\"me-2\"><circle cx=\"12\" cy=\"12\" r=\"10\"></circle><path d=\"M8 14s1.5 2 4 2 4-2 4-2\"></path><line x1=\"9\" y1=\"9\" x2=\"9.01\" y2=\"9\"></line><line x1=\"15\" y1=\"9\" x2=\"15.01\" y2=\"9\"></line></svg><strong>OK!</strong> 添加产品成功.</div>\n")
        } else {
            var divElement = $("<div style='width: 380px' class=\"alert alert-light \">\n" +
                "    <svg viewBox=\"0 0 24 24\" width=\"24\" height=\"24\" stroke=\"currentColor\" stroke-width=\"2\" fill=\"none\" stroke-linecap=\"round\" stroke-linejoin=\"round\" class=\"me-2\"><polygon points=\"7.86 2 16.14 2 22 7.86 22 16.14 16.14 22 7.86 22 2 16.14 2 7.86 7.86 2\"></polygon><line x1=\"15\" y1=\"9\" x2=\"9\" y2=\"15\"></line><line x1=\"9\" y1=\"9\" x2=\"15\" y2=\"15\"></line></svg>\n" +
                "    <strong>Refuse!</strong> TheProductionPlanHasBeenRejected..\n" +
                "</div>")
        }
        let number = parseInt($(window).width()) / 2 - 80;
        let number1 = parseInt($(window).height()) / 2 - 180;
        divElement.css({ // 消息框的定位样式
            "position": "absolute",
            "left": number,
            "top": number1
        });
        //divElement.text(msg); // 设置消息框的内容
        // 消息框添加可以关闭按钮
        var closeBtn = $('<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true"><i class="bi bi-x"></i></span></button>');
        $(divElement).append(closeBtn);
        // 消息框放入到页面中
        $('body').append(divElement);
        return divElement;
    },

    success: function () {
        if (typeof (type) == "undefined") { // 未传入type则默认为success类型的消息框
            type = "success";
        }
        var divElement = $("<div class=\"col-xl-3\">\n" +
            "<div class=\"alert alert-success left-icon-big alert-dismissible fade show\">\n" +
            "<div class=\"media\">\n" +
            "<div class=\"alert-left-icon-big\">\n" +
            "    <span><i class=\"mdi mdi-check-circle-outline\"></i></span>\n" +
            "</div>\n" +
            "<div class=\"media-body\">\n" +
            "    <h5 class=\"mt-1 mb-2\">productionPlanApproved!</h5>\n" +
            "</div>\n" +
            "</div>\n" +
            "</div>\n" +
            "</div>")
        let number = parseInt($(window).width()) / 2 - 120;
        let number1 = parseInt($(window).height()) / 2 - 180;
        divElement.css({ // 消息框的定位样式
            "position": "absolute",
            "left": number,
            "top": number1
        });
        //divElement.text(msg); // 设置消息框的内容
        // 消息框添加可以关闭按钮

        // 消息框放入到页面中
        $('body'
        ).append(divElement);
        return divElement;
    },


    /**
     * 短暂显示后上浮消失的消息框
     * @param msg 消息内容
     * @param type 消息框类型
     */
    message: function (msg, type) {
        if (msg == 1 || msg == 2) {
            var divElement = commonUtil.alert(msg, type); // 生成Alert消息框
        } else {
            var divElement = commonUtil.success()
        }
        var isIn = false; // 鼠标是否在消息框中

        divElement.on({ // 在setTimeout执行之前先判定鼠标是否在消息框中
            mouseover: function () {
                isIn = true;
            },
            mouseout: function () {
                isIn = false;
            }
        });

        // 短暂延时后上浮消失
        setTimeout(function () {
            var IntervalMS = 20; // 每次上浮的间隔毫秒
            var floatSpace = 60; // 上浮的空间(px)
            var nowTop = divElement.offset().top; // 获取元素当前的top值
            var stopTop = nowTop - floatSpace;    // 上浮停止时的top值
            divElement.fadeOut(IntervalMS * floatSpace); // 设置元素淡出

            var upFloat = setInterval(function () { // 开始上浮
                if (nowTop >= stopTop) { // 判断当前消息框top是否还在可上升的范围内
                    divElement.css({"top": nowTop--}); // 消息框的top上升1px
                } else {
                    clearInterval(upFloat); // 关闭上浮
                    divElement.remove();    // 移除元素
                }
            }, IntervalMS);

            if (isIn) { // 如果鼠标在setTimeout之前已经放在的消息框中，则停止上浮
                clearInterval(upFloat);
                divElement.stop();
            }

            divElement.hover(function () { // 鼠标悬浮时停止上浮和淡出效果，过后恢复
                clearInterval(upFloat);
                divElement.stop();
            }, function () {
                divElement.fadeOut(IntervalMS * (nowTop - stopTop)); // 这里设置元素淡出的时间应该为：间隔毫秒*剩余可以上浮空间
                upFloat = setInterval(function () { // 继续上浮
                    if (nowTop >= stopTop) {
                        divElement.css({"top": nowTop--});
                    } else {
                        clearInterval(upFloat); // 关闭上浮
                        divElement.remove();    // 移除元素
                    }
                }, IntervalMS);
            });
        }, 1500);
    }
}
